package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.annotation.AppSecurity;
import com.ruoyi.app.util.ReqMsg;
import com.ruoyi.app.util.RspMsg;
import com.ruoyi.bizsys.domain.AppUserMark;
import com.ruoyi.bizsys.domain.BankAccount;
import com.ruoyi.bizsys.service.IAppUserMarkService;
import com.ruoyi.bizsys.vo.BankAccountVo;
import com.ruoyi.business.common.AsyncService;
import com.ruoyi.business.service.IAppBankAccountService;
import com.ruoyi.business.service.IBankCardService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.enums.UserTagEnum;
import com.ruoyi.common.utils.Func;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * swagger app前端公开接口
 *
 * @author ruoyi
 * @date 2021-12-28
 */
@Api("app前端接口")
@RestController
@Slf4j
@RequestMapping("/app/bankAccount")
public class AppBankAccountController extends BaseController {

    @Autowired
    private IAppBankAccountService appBankAccountService;

    @Autowired
    AsyncService asyncService;

    @Autowired
    IAppUserMarkService appUserMarkService;


    @Autowired
    IBankCardService bankCardService;

    /**
     * Account 新增
     */
    @AppSecurity
    @ApiOperation("Account 新增")
    @Log(title = "Account 新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public RspMsg bankAccountAdd(@Validated @RequestBody ReqMsg reqMsg) {
        BankAccount bankAccount = JSON.parseObject(reqMsg.getData().toString(), BankAccount.class);
        //获取当前用户userId 和custNo
        String userId = getUserId();
        String custNo = getCustNo();
        if (null != userId && null != custNo) {
            bankAccount.setCustNo(custNo);
            bankAccount.setUserId(userId);
            appBankAccountService.insertBankAccount(bankAccount);
            //修改用户标签
            asyncService.updateAppUserMark(userId,"bankAccount");

            return RspMsg.RspMsgOk();
        }
        return RspMsg.fail(RspEnmu.ILLEGAL_ARGUMENT);
    }

    /**
     * account 列表查询
     */
    @AppSecurity
    @ApiOperation("Account 列表查询")
    @Log(title = "Account 列表查询", businessType = BusinessType.GET)
    @PostMapping("/list")
    public RspMsg getBankAccountList(@Validated @RequestBody ReqMsg reqMsg) {
        BankAccount bankAccount = new BankAccount();
        bankAccount.setUserId(getUserId());
        List<BankAccountVo> bankAccountVoList = appBankAccountService.selectBankAccountListByUserId(bankAccount);
        return RspMsg.RspMsgOk(bankAccountVoList);
    }

    @AppSecurity
    @ApiOperation("设置默认account")
    @Log(title = "设置默认account", businessType = BusinessType.UPDATE)
    @PostMapping("/default")
    public RspMsg setDefaultAccount(@Validated @RequestBody ReqMsg reqMsg) {
        BankAccount bankAccount = JSON.parseObject(reqMsg.getData().toString(), BankAccount.class);
        BankAccount account = new BankAccount();
        account.setUserId(getUserId());
        account.setIsDefault("1");
        int bankAccountSize = appBankAccountService.selectCountByUserId(account);
        if (bankAccountSize > 0) {
            account.setIsDefault("0");
            appBankAccountService.updateBankAccountIsDefault(account);
        }
        bankAccount.setIsDefault("1");
        int result = appBankAccountService.updateBankAccountIsDefault(bankAccount);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("status", 200);
            jsonObject.put("msg", "success");
        }
        return RspMsg.RspMsgOk(jsonObject);
    }

    @AppSecurity
    @ApiOperation("绑卡策略检查")
    @Log(title = "绑卡策略检查", businessType = BusinessType.OTHER)
    @PostMapping("/strategyCheck")
    public RspMsg cardStrategyCheck(@Validated @RequestBody ReqMsg reqMsg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("needBindCardNum", bankCardService.custNeedBindCardNum(getCustomerInfo()));
        return RspMsg.RspMsgOk(jsonObject);
    }

}
